package com.ebelter.temperaturegun.ui.activity;



import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CountDownTextView;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.model.http.TGNetUtils;

public class ChangePasswordActivity1 extends TBaseActivity implements View.OnClickListener {


    public static final String TAG = "ChangePasswordActivity1";
    private ImageView top_iv;
    private TextView top_tv1;
    private EditText et_email_changepsw1;
    private CountDownTextView changepsw1_countDownTv;
    private EditText et_emailcode_changepsw1;
    private TextView btn_next;

    @Override
    protected int setContentViewId() {
        return R.layout.change_password;
    }



    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);

        top_tv1=findViewById(R.id.top_tv1);

        top_tv1.setText(R.string.xgmm);
        ViewUtils.displayView(top_tv1);

        et_email_changepsw1=findViewById(R.id.et_email_changepsw1);
        et_emailcode_changepsw1=findViewById(R.id.et_emailcode_changepsw1);
        changepsw1_countDownTv=findViewById(R.id.changepsw1_countDownTv);
        btn_next = findViewById(R.id.btn_next);
        btn_next.setEnabled(false);


        ViewUtils.setTextViewStr(et_email_changepsw1, TemUtils.getUerEmail());

        top_iv.setOnClickListener(this);
        changepsw1_countDownTv.setOnClickListener(this);


    }

    public void next(View view) {
        String email = et_email_changepsw1.getText().toString().trim();
        String code = et_emailcode_changepsw1.getText().toString().trim();

        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }

        if (TextUtils.isEmpty(code)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        Bundle bundle=new Bundle();
        bundle.putString(ChangePasswordActivity2.EMAIL,email);
        bundle.putString(ChangePasswordActivity2.EMAILCODE,code);
        startActivity(ChangePasswordActivity2.class,bundle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_iv:
                finish();
                break;
            case R.id.changepsw1_countDownTv:
                countDownTextViewClick();
                break;
        }
    }

    private void countDownTextViewClick() {

        String email=et_email_changepsw1.getText().toString().trim();
        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
        }else {

            if (!NetUtils.available()){
                ToastUtil.show(R.string.Network_error);
                return;
            }

            //showLoadingDialog(StringUtils.getResStr(R.string.Request_email_verification_code));
            TGNetUtils.getInstance().getEmailValidateCodeRequest(this, email, 2, new HttpResponse<getEmailValidateCodeResponse>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse,String s) {
                    // cancelLoadingDialog();
                    LogUtils.i(TAG,"-- s = " + s);
                    if (isRequestSuccess){

                        if (getEmailValidateCodeResponse.resultCode==0){
                            if (changepsw1_countDownTv!=null)changepsw1_countDownTv.click();//说明请求成功，按钮进入倒计时模式
                            ToastUtil.show(R.string.The_email_has_been_sent);
                            btn_next.setEnabled(true);
                        }else if (getEmailValidateCodeResponse.resultCode==102){//邮箱已经注册
                            ToastUtil.show(R.string.Email_has_been_registered);
                        }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                            ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                        }else {
                            ToastUtil.show(R.string.Failed_to_send_mail);
                        }
                    }else {
                        ToastUtil.show(R.string.Network_connection_error);
                    }
                }
            });
        }

    }
}
